package net.homelinux.chaoswg.io.renga.krypto.animations.Xor;

import javax.swing.JLabel;

import net.homelinux.chaoswg.io.renga.core.animations.Animation;
import net.homelinux.chaoswg.io.renga.core.animations.State;

public class XorAnimation extends Animation {
    XorState initialState;
    XorStateChanger xorChanger;
    
    JLabel firstOperandsDisplay [];
    JLabel secondOperandsDisplay[];
    JLabel resultDisplay [];
    
    public XorAnimation (XorState initialState) {
        this.initialize(initialState);
    }
    
    public XorAnimation () {
        this.initialize(new XorState());
    }
    
    private void initialize(final State initialState) throws IllegalArgumentException{
        if ( !(initialState instanceof XorState)){
            throw new IllegalArgumentException ();            
        }
        
        this.initialState = ((XorState) initialState);
        this.xorChanger = new XorStateChanger (this.initialState);
    }

    @Override
    public synchronized void start() {
        Thread t = new Thread (xorChanger);
        t.setDaemon(true);
        t.start();
        
        try {
            t.join();
        } catch (InterruptedException iexcp){ 
            iexcp.printStackTrace();
        }
        xorChanger.close ();
        finished = true;
        setChanged();
        notifyObservers();
    }
    
    @Override
    public void setOperand(int i, Object operand) {
        initialState.setOperand(i, operand);
    }

}
